﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmKhachHang : Form
    {
        public static FrmKhachHang frm = null;
        private IList<KHACHHANG> lstkhachhang;
        private bool ThemOrCapnhat = false;

        public FrmKhachHang()
        {
            InitializeComponent();
            frm = this;
            InitKhachHang();
        }

        public FrmKhachHang(bool them)
        {
            InitializeComponent();
            frm = this;
            InitKhachHang();
            if (them == true)
            {
                btnthem.PerformClick();
            }
        }

        private void InitKhachHang()
        {
            KHACHHANG[] outkh;
            var client = new ServiceQLKSClient();
            var error = client.doparserKhacHang(out outkh, OPTCodeQLKS.OPTCode_KHACKHANG_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_KHACHHANG_GETALL_SUCCESS)
            {
                lstkhachhang=outkh.ToList();
                kHACHHANGBindingSource.DataSource = lstkhachhang;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InsertOrUpdate()
        {
            KHACHHANG kh = new KHACHHANG();
            if (txtTen.Text == null)
            {
                MessageBox.Show("Chưa nhập tên", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTen.Focus();
                return;
            }
            if (txtCMND.Text == null)
            {
                MessageBox.Show("Chưa nhập CMND", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCMND.Focus();
                return;
            }
            if (ThemOrCapnhat == true)
            {
                kh.HOTENKH = txtTen.Text;
                kh.CMND=txtCMND.Text;
                kh.DIACHIKH=txtDChi.Text;
                kh.DIENTHOAIKH=txtDThoai.Text;
                if (optNam.Checked == true)
                {
                    kh.GIOITINHKH = true;
                }
                else
                {
                    kh.GIOITINHKH = false;
                }
            }
            else
            {
                kh.HOTENKH = txtTen.Text;
                kh.CMND = txtCMND.Text;
                kh.DIACHIKH = txtDChi.Text;
                kh.DIENTHOAIKH = txtDThoai.Text;
                if (optNam.Checked == true)
                {
                    kh.GIOITINHKH = true;
                }
                else
                {
                    kh.GIOITINHKH = false;
                }
                kh.MSKH = decimal.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            }
            KHACHHANG[] outkh;
            var client = new ServiceQLKSClient();
            var error = client.doparserKhacHang(out outkh, OPTCodeQLKS.OPTCode_KHACKHANG_INSERTUPDATE, kh);
            if (error == ErrorCodeQLKS.CODE_KHACHHANG_INSERTUPDATE_SUCCESS)
            {
                InitKhachHang();
                ThemOrCapnhat = false;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void DeleteKH()
        {
            KHACHHANG[] outkh;
            var item=dataGridView1.CurrentRow.DataBoundItem as KHACHHANG;
            var client = new ServiceQLKSClient();
            var error = client.doparserKhacHang(out outkh, OPTCodeQLKS.OPTCode_KHACKHANG_DELETE, item);
            if (error == ErrorCodeQLKS.CODE_KHACHHANG_DELETE_SUCCESS)
            {
                InitKhachHang();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void FrmKhachHang_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = dataGridView1.CurrentRow.DataBoundItem as KHACHHANG;
            if (item != null)
            {
                txtTen.Text = item.HOTENKH;
                txtCMND.Text = item.CMND;
                txtDChi.Text = item.DIACHIKH;
                txtDThoai.Text = item.DIENTHOAIKH;
                if (item.GIOITINHKH == true)
                {
                    optNam.Checked = true;
                }
                else
                {
                    optNu.Checked = true;
                }
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            InsertOrUpdate();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            txtTen.Clear();
            txtTen.Focus();
            txtDThoai.Clear();
            txtDChi.Clear();
            txtCMND.Clear();
            ThemOrCapnhat = true;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DeleteKH();
        }
    }
}
